﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAPI;

namespace Client_test
{
    public partial class ChatWindow : Form
    {
        string user = "admin";
        private delegate void UpdateTextboxHandler(string chatlog);
        public ChatWindow()
        {
            InitializeComponent();
            ClientAPI.ClientAPI.Instance.updateChatHandler += updateChatPage;
        }

        private void sendBTN_Click(object sender, EventArgs e)
        {
            if (ClientAPI.ClientAPI.Instance.IsConnected())
            {
                if (sendTXT.Text != "" || sendTXT.Text != null)
                {
                    ClientAPI.ClientAPI.Instance.SendChatMessage(user, sendTXT.Text);
                }

                sendTXT.Text = "";
            }
            else
                MessageBox.Show("warning not connected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }

        private void updateChatPage(object sender, UpdateChatEventArgs eargs)
        {
            if (chatPage.InvokeRequired)
                chatPage.Invoke(new UpdateTextboxHandler(UpdateTextBox), ClientAPI.ClientAPI.Instance.GetChatLog("admin"));
            else
                UpdateTextBox(ClientAPI.ClientAPI.Instance.GetChatLog("admin"));            
        }

        private void UpdateTextBox(string message)
        {
            chatPage.Text = message;
        }
    }
}
